package com.fqy.thread02.number;

import java.util.concurrent.TimeUnit;

/**
 * User: fqy
 * Date: 2021/9/1
 * Time: 14:58
 * Describe: i++数据不安全问题
 * 解决方法：
 *      1.synchronized
 *      2.Lock
 *      3.CAS
 */
public class NumberUnsafeDemo {
    public static void main(String[] args) {
        MyNumber myNumber = new MyNumber();
        for (int i = 0; i < 200; i++) {
            new Thread(()->{
                for (int j = 0; j <200; j++) {
                    myNumber.setNum();
                }
            }, String.valueOf(i)).start();
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(myNumber.getNum());
    }
}
